package com.tentact.dao;

import com.tentact.vo.Chf_DEId;
import com.tentact.vo.Employee;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Chf_DEIdDao {
    //两表联查部门与员工
    public List<Chf_DEId> select(){
        String sql="SELECT d.DepartmentId, d.DepartmentName,e.EmployeeId,e.EmployeeName FROM department d, employee e WHERE d.DepartmentId=e.DepartmentId";
        Object[] para=null;
        RowMapper<Chf_DEId> rm=rs -> {
            Chf_DEId deId=new Chf_DEId();

            try {


                deId.setDepartmentId(rs.getInt("DepartmentId"));
                deId.setDepartmentName(rs.getString("DepartmentName"));
                deId.setEmployeeId(rs.getInt("EmployeeId"));
                deId.setEmployeeName(rs.getString("EmployeeName"));

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return deId;
        };
        return  Dbutil.executeQuery(sql,para,rm);

    }
    //通过Id查找员工
    public List<Chf_DEId> selectEmpById(int id){
        List<Chf_DEId> deid_list=new ArrayList<>();
        Chf_DEId deid=null;
        for (Chf_DEId e:select()){
            if (e.getDepartmentId()==id){
                deid=new Chf_DEId(e.getDepartmentId(),e.getDepartmentName(),e.getEmployeeId(),e.getEmployeeName());
                deid_list.add(deid);
            }

        }
        return deid_list;
    }
}
